package com.itx.gatewayx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itx.gatewayx.common.R;
import com.itx.gatewayx.domain.NewsComment;
import com.itx.gatewayx.service.NewsCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 新闻评论控制器
 *
 * @author itx
 */
@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
public class NewsCommentController {
    
    private final NewsCommentService commentService;
    
    /**
     * 分页获取新闻评论
     *
     * @param current 当前页
     * @param size 每页大小
     * @param newsId 新闻ID
     * @return 评论分页列表
     */
    @GetMapping
    public R<IPage<NewsComment>> getCommentList(
            @RequestParam(value = "current", defaultValue = "1") Long current,
            @RequestParam(value = "size", defaultValue = "10") Long size,
            @RequestParam("newsId") String newsId) {
        IPage<NewsComment> commentPage = commentService.getCommentList(current, size, newsId);
        return R.ok(commentPage);
    }
    
    /**
     * 添加评论
     *
     * @param comment 评论信息
     * @return 是否成功
     */
    @PostMapping
    public R<Boolean> addComment(@RequestBody @Validated NewsComment comment) {
        boolean result = commentService.addComment(comment);
        return result ? R.ok(true) : R.fail("添加评论失败");
    }
    
    /**
     * 回复评论
     *
     * @param comment 评论信息
     * @return 是否成功
     */
    @PostMapping("/reply")
    public R<Boolean> replyComment(@RequestBody @Validated NewsComment comment) {
        boolean result = commentService.replyComment(comment);
        return result ? R.ok(true) : R.fail("回复评论失败");
    }
    
    /**
     * 删除评论
     *
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @DeleteMapping("/{commentId}")
    public R<Boolean> deleteComment(
            @PathVariable Long commentId,
            @RequestParam("userId") Long userId) {
        boolean result = commentService.deleteComment(commentId, userId);
        return result ? R.ok(true) : R.fail("删除评论失败");
    }
    
    /**
     * 获取评论回复列表
     *
     * @param parentId 父评论ID
     * @return 回复列表
     */
    @GetMapping("/replies/{parentId}")
    public R<NewsComment> getCommentWithReplies(@PathVariable Long parentId) {
        NewsComment comment = commentService.getCommentWithReplies(parentId);
        return comment != null ? R.ok(comment) : R.notFound("评论不存在");
    }
} 